/*
 * DatabaseUtils.fx
 *
 * Created on Feb 22, 2010, 10:40:43 PM
 */
package com.gpsbuddy.utils;

import javafx.io.Storage;
import javafx.io.Resource;
import java.io.InputStream;
import javafx.data.pull.PullParser;
import java.lang.NullPointerException;
import javafx.data.pull.Event;
import com.gpsbuddy.data.Property;
import java.io.IOException;

/**
 * @author jonycus
 */
public var storage: Storage;
public var prop: Property;
public var props: Property[];
public var initialized: Boolean = false;

public function initiliazeDatabase(): Boolean {
    storage = Storage {
        source: "test.db";
    }
    var resource: Resource = storage.resource;
    var inputStream: InputStream = resource.openInputStream();
    readData(inputStream);
    println("Initilatization done!!!");
    inputStream.close();
    loadInitialProcesses();
    return initialized;
    }

function initStorage() {
    println("Initializing Storage…");
    var out = storage.resource.openOutputStream(true);
    println("Stream opened…");
    var dummyRoot = "<props></props>\n";
    try {
        out.write(dummyRoot.getBytes());
        out.close();
        println("Stream closed…");
    } catch (ex: IOException) {
        ex.printStackTrace();
    }
    initialized = true;
}

function parseCallback(e: Event) {
    if (e.type == PullParser.START_ELEMENT) {
        if (e.qname.name == "prop" and e.level == 1) {
            prop = Property { };
        }
    } else if (e.type == PullParser.END_ELEMENT) {
        if (e.qname.name == "prop" and e.level == 1) {
            insert prop into props;
        }
    } else if (e.type == PullParser.TEXT) {
        if (e.qname.name == "name" and e.level == 2) {
            prop.name = e.text;
        }
        if (e.qname.name == "value" and e.level == 2) {
            prop.value = e.text;
        }
    }
}

public function addProp(name: String, value: String): Void {
    prop = Property {
        name: name;
        value: value;
    }
    insert prop into props;
}

public function hasProp(name: String): Boolean {
    var retVal: Boolean = false;
    for (prop in props) {
        if (prop.name.equals(name)) {
            retVal = true;
            return retVal;
        }
    }
    return retVal;
}

public function updateProp(name: String, value: String): Void {
    for (prop in props) {
        if (prop.name.equals(name)) {
            prop.value = value;
            break;
        }
    }
}

public function getPropValue(name: String): String {
    var retValue: String = "";
    for (prop in props) {
        if (prop.name.equals(name)) {
            retValue = prop.value;
            return retValue;
        }
    }
    return retValue;
}

public function storeData(): Void {
    var out = storage.resource.openOutputStream(true);
    try {
        out.write("<props>\n".getBytes());
        for (prop in props) {
            out.write(prop.toXml().getBytes());
        }
        out.write("</props>\n".getBytes());
        out.close();
    } catch (ex: IOException) {
        ex.printStackTrace();
    }
}

public function readData(inn: InputStream): Void {
    var parser = PullParser {
                input: inn;
                onEvent: parseCallback;
            }
    try {
        parser.parse();
        initialized = true;
        } catch (npe: NullPointerException) {
        initStorage();
        }
}

public function printData() {
    var is = storage.resource.openInputStream();
    var par = PullParser {
                input: is;
                onEvent: function (e: Event) {
                    if (e.type == PullParser.START_ELEMENT) {
                        if (e.qname.name == "prop" and e.level == 1) {
                            println("<prop>");
                        }
                    } else if (e.type == PullParser.END_ELEMENT) {
                        if (e.qname.name == "prop" and e.level == 1) {
                            println("</prop>")
                        }
                    } else if (e.type == PullParser.TEXT) {
                        if (e.qname.name == "name" and e.level == 2) {
                            println("<name>{e.text}</name>")
                        }
                        if (e.qname.name == "value" and e.level == 2) {
                            println("<value>{e.text}</value>")
                        }
                    }
                }
            }
    par.parse();
}

public function loadInitialProcesses() {
    println("Loading initial processes...");
    if (DatabaseUtils.hasProp("autoConnect") and DatabaseUtils.getPropValue("autoConnect").equals("true")) {
        println("Auto-logging to servce...");
        ServiceUtils.loginToService(null, null);
    }
}

